package com.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.aspectj.util.FileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

//生成excel文件工具类
public class PoiUtil
{
    //列宽
    private int columWidth;
    //内容行高
    private short contentHeight;

    public PoiUtil(){};

    public PoiUtil( int columWidth , short contentHeight )
    {
        this.columWidth = columWidth;
        this.contentHeight = contentHeight;
    }

    public String createExcel(
            String excelTitle , LinkedHashMap<String,String> columnTitle ,
            ArrayList<Map<String,Object>> content , String path )
    {

        String fileName = excelTitle + ".xls" ;
        //创建excel
        HSSFWorkbook wb = new HSSFWorkbook( );
        //创建sheet
        HSSFSheet sheet = wb.createSheet( excelTitle );

        //创建标题字体
        HSSFFont titleFont = wb.createFont();
        //设置字高
        titleFont.setFontHeightInPoints( (short)14 );
        //设置粗体
        titleFont.setBold( true );

        //创建列标题字体
        HSSFFont columnFont = wb.createFont();
        //设置粗体
        columnFont.setBold( true );

        //创建标题样式
        HSSFCellStyle titleStyle = wb.createCellStyle();
        //设置水平居中
        titleStyle.setAlignment( HorizontalAlignment.CENTER );
        //设置垂直居中
        titleStyle.setVerticalAlignment( VerticalAlignment.CENTER  );
        //设置列标题粗体
        titleStyle.setFont( titleFont );

        //创建列标题样式
        HSSFCellStyle columnStyle = wb.createCellStyle();
        columnStyle.setAlignment( HorizontalAlignment.CENTER );
        columnStyle.setVerticalAlignment( VerticalAlignment.CENTER  );
        columnStyle.setFont( columnFont );

        //创建内容样式
        HSSFCellStyle contentStyle = wb.createCellStyle();
        contentStyle.setAlignment( HorizontalAlignment.CENTER );
        contentStyle.setVerticalAlignment( VerticalAlignment.CENTER  );
        //设置自动换行
        contentStyle.setWrapText(true);

        //创建第一行(excel标题)
        HSSFRow rowFirst = sheet.createRow( 0 );
        //设置行高
        rowFirst.setHeight( (short)800 );
        HSSFCell cellTile = rowFirst.createCell( 0 );
        //填充标题行
        cellTile.setCellValue( excelTitle );
        cellTile.setCellStyle( titleStyle );
        //获取列数
        int columnNum = columnTitle.size();
        //单元格合并 参数：起始行 终止行 起始列 终止例
        CellRangeAddress region1 = new CellRangeAddress(0, 0, (short)0, (short)(columnNum-1) );
        sheet.addMergedRegion( region1 );

        //创建列名行
        HSSFRow rowSecond = sheet.createRow( 1 );
        rowSecond.setHeight( (short)500 );

        //填充列标题
        int titleCount = 0;
        for( String x : columnTitle.keySet() )
        {
            //设置列宽
            sheet.setColumnWidth(titleCount, columWidth );
            HSSFCell cell = rowSecond.createCell( titleCount++ );
            cell.setCellValue( x );
            cell.setCellStyle( columnStyle );
        }
        //填充内容(内容从第2行起填充)
        for( int i = 0 ; i < content.size() ; i++ )
        {
            HSSFRow contentRow = sheet.createRow( i + 2 );
            contentRow.setHeight( contentHeight );
            Map<String,Object> x = content.get( i );
            int columnCount = 0;
            for( String column : columnTitle.values() )
            {
                HSSFCell cell = contentRow.createCell( columnCount++ );
                String cellContent = (String)x.get( column );
                cell.setCellValue( cellContent );
                cell.setCellStyle( contentStyle );
            }
        }
        try
        {
            FileOutputStream fout = new FileOutputStream( path + File.separator + fileName );
            wb.write(fout);
            fout.flush();
            fout.close();
        }
        catch ( IOException e)
        {
            System.out.println( "PoiUtil的createExcel方法出错" );
            e.printStackTrace();
        }

        return fileName;
    }

}
